package com.cpecc.controller.wuzi;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.cpecc.mail.SendMailThread;
import com.cpecc.manager.system.EmailManager;
import com.cpecc.liucheng.manager.HuanJieManager;
import com.cpecc.manager.system.RootManager;
import com.cpecc.manager.system.ShiYongZTManager;
import com.cpecc.manager.system.ToDoManager;
import com.cpecc.manager.system.UserManager;
import com.cpecc.manager.system.YouJianManager;
import com.cpecc.manager.wuzi.BaoFeiManager;
import com.cpecc.manager.wuzi.BuPeiManager;
import com.cpecc.manager.wuzi.HuanHuoJLManager;
import com.cpecc.manager.wuzi.HuanHuoManager;
import com.cpecc.manager.wuzi.ShenQingJLManager;
import com.cpecc.manager.wuzi.ShenQingManager;
import com.cpecc.manager.wuzi.TuiHuoManager;
import com.cpecc.manager.wuzi.WeiXiuJLManager;
import com.cpecc.manager.wuzi.WeiXiuManager;
import com.cpecc.manager.wuzi.WuZiManager;
import com.cpecc.manager.wuzi.YiJiaoManager;
import com.cpecc.model.system.Email;
import com.cpecc.model.system.ToDo;
import com.cpecc.model.system.User;
import com.cpecc.model.system.YouJian;
import com.cpecc.model.wuzi.HuanHuo;
import com.cpecc.model.wuzi.HuanHuoJL;
import com.cpecc.model.wuzi.ShenQing;
import com.cpecc.model.wuzi.ShenQingJL;
import com.cpecc.model.wuzi.WeiXiu;
import com.cpecc.model.wuzi.WeiXiuJL;
import com.cpecc.model.wuzi.WuZi;

@Controller
@RequestMapping(value = "/wuzi")
public class HuanHuoController {
	
	@Autowired
	private UserManager userManager;
	
	@Autowired
	private ShenQingManager shenQingManager;
	
	@Autowired
	private HuanJieManager huanJieManager;
	
	@Autowired
	private HuanHuoManager huanHuoManager;
	
	@Autowired
	private HuanHuoJLManager huanHuoJLManager;
	
	@Autowired
	private ShenQingJLManager shenQingJLManager;
	
	@Autowired
	private WeiXiuManager weiXiuManager;
	
	@Autowired
	private WeiXiuJLManager weiXiuJLManager;
	
	@Autowired
	private ShiYongZTManager shiYongZTManager;
	
	@Autowired
	private WuZiManager wuZiManager;
	
	@Autowired
	private BaoFeiManager baoFeiManager;	
	
	@Autowired
	private TuiHuoManager tuiHuoManager;
	
	@Autowired
	private BuPeiManager buPeiManager;
	
	@Autowired
	private YiJiaoManager yiJiaoManager;
	
	@Autowired
	private ToDoManager toDoManager;
	
	@Autowired
	private HuanHuoManager huanHuoManger;
	
	@Autowired
	private YouJianManager youJianManager;
	
	@Autowired
	private RootManager rootManager;
	
	@Autowired
	private EmailManager emailManager;
	
	@RequestMapping(value = "/cghhList")
	public ModelAndView cghhList(){
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("huanHuoList", huanHuoManager.getAllHuanHuoSQ2CMR(1,user));
		mav.setViewName("shenqing/cghhListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/caiGouHuanHuo")
	public ModelAndView caiGouHuanHuo(String id){
		ModelAndView mav=new ModelAndView();
		mav.addObject("shenQingId", id);
		mav.addObject("lx", 1);
		mav.setViewName("wuzi/huanHuo");
		return mav;
	}
	
	@RequestMapping(value = "/weiXiuHuanHuo")
	public ModelAndView weiXiuHuanHuo(String id){
		ModelAndView mav=new ModelAndView();
		mav.addObject("weiXiuId", id);
		mav.addObject("lx", 2);
		mav.setViewName("wuzi/huanHuo");
		return mav;
	}
	
	@RequestMapping(value = "/huanHuoShenQing")
	public void huanHuoShenQing(@RequestParam(required=false)String shenQingId,@RequestParam(required=false)String weiXiuId,int lx,String huanHuoYY,HttpServletResponse response){
		String wuZiId="";
		if(shenQingId!=null && !shenQingId.equals("")){
			wuZiId=shenQingManager.get(shenQingId).getWuZi().getId();
		}else if(weiXiuId!=null && !weiXiuId.equals("")){
			wuZiId=weiXiuManager.get(weiXiuId).getWuZi().getId();
		}
		if(yiJiaoManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		else if(baoFeiManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(huanHuoManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(tuiHuoManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(buPeiManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		HuanHuo huanHuo=new HuanHuo();
		huanHuo.setHuanHuoYY(huanHuoYY);
		huanHuo.setLiuChengLX(lx);
		huanHuo.setShenQingRQ(new Date());
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		huanHuo.setUser(user);
		if(lx==1){
			ShenQing shenQing=shenQingManager.get(shenQingId);
			shenQing.setHuanJie(huanJieManager.get(202));
			shenQingManager.cover(shenQing);
			ShenQingJL shenQingJL=new ShenQingJL();
			shenQingJL.setCaoZuoRQ(new Date());
			shenQingJL.setHuanJie(huanJieManager.get(201));
			shenQingJL.setShenQing(shenQing);
			shenQingJL.setUser(user);
			shenQingJL.setPiFuLX("提交采购换货申请");
			shenQingJL.setPiFuNR("提交");
			shenQingJLManager.save(shenQingJL);
			huanHuo.setWuZi(shenQing.getWuZi());
			huanHuo.setShenQing(shenQing);
			toDoManager.deleteList(shenQing);
		}else if(lx==2){
			WeiXiu weiXiu=weiXiuManager.get(weiXiuId);
			weiXiu.setHuanJie(huanJieManager.get(202));
			weiXiuManager.cover(weiXiu);
			WeiXiuJL weiXiuJL=new WeiXiuJL();
			weiXiuJL.setCaoZuoRQ(new Date());
			weiXiuJL.setHuanJie(huanJieManager.get(201));
			weiXiuJL.setPiFuLX("提交维修换货申请");
			weiXiuJL.setPiFuNR("提交");
			weiXiuJL.setWeiXiu(weiXiu);
			weiXiuJL.setUser(user);
			weiXiuJLManager.save(weiXiuJL);
			huanHuo.setWuZi(weiXiu.getWuZi());
			huanHuo.setWeiXiu(weiXiu);
			toDoManager.deleteList(weiXiu);
		}
		huanHuo.setHuanJie(huanJieManager.get(202));
		huanHuoManager.save(huanHuo);
		WuZi wuZi=huanHuo.getWuZi();
		wuZi.setShiYongZT(shiYongZTManager.get(114));
		wuZiManager.cover(wuZi);
		HuanHuoJL huanHuoJL=new HuanHuoJL();
		huanHuoJL.setCaoZuoRQ(new Date());
		huanHuoJL.setHuanHuo(huanHuo);
		huanHuoJL.setHuanJie(huanJieManager.get(201));
		huanHuoJL.setPiFuLX("提交申请");
		huanHuoJL.setPiFuNR("提交");
		huanHuoJL.setUser(user);
		huanHuoJLManager.save(huanHuoJL);
		//\\发送邮件
		YouJian youJian=youJianManager.get(202);
		String root=rootManager.getRoot();
		Email email=emailManager.getEmail();
		if(youJian!=null && youJian.getState()==1){
			SendMailThread st=new SendMailThread(email, root, 202, wuZi.getCaiMaiR(), youJian);
			st.setWuZi(wuZi);
			st.setHuanHuo(huanHuo);
			Thread t=new Thread(st);
			t.start();
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/caiGouHuanHuoTY")
	public void caiGouHuanHuoTY(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		HuanHuo huanHuo=huanHuoManager.get(id);
		ShenQing shenQing=huanHuo.getShenQing();
		shenQing.setHuanJie(huanJieManager.get(203));
		shenQingManager.cover(shenQing);
		ShenQingJL shenQingJL=new ShenQingJL();
		shenQingJL.setShenQing(shenQing);
		shenQingJL.setCaoZuoRQ(new Date());
		shenQingJL.setHuanJie(huanJieManager.get(202));
		shenQingJL.setPiFuLX("通过");
		shenQingJL.setPiFuNR("同意");
		shenQingJL.setUser(user);
		shenQingJLManager.save(shenQingJL);
		huanHuo.setYuanChanPinXLH(shenQing.getWuZi().getChanPinXLH());
		huanHuo.setHuanJie(huanJieManager.get(203));
		huanHuoManager.cover(huanHuo);
		HuanHuoJL huanHuoJL=new HuanHuoJL();
		huanHuoJL.setCaoZuoRQ(new Date());
		huanHuoJL.setHuanHuo(huanHuo);
		huanHuoJL.setHuanJie(huanJieManager.get(202));
		huanHuoJL.setUser(user);
		huanHuoJL.setPiFuLX("采买人同意换货");
		huanHuoJL.setPiFuNR("同意");
		huanHuoJLManager.save(huanHuoJL);
		toDoManager.delete(shenQing, user);
		String root=rootManager.getRoot();
		Email email=emailManager.getEmail();
		//低值管理员发放
		for(User user1:userManager.getGZGLY()){
			ToDo toDo=new ToDo();
			toDo.setUser(user1);
			toDo.setHuanJie(huanJieManager.get(105));
			toDo.setCreateDate(new Date());
			toDo.setTask("物资发放");
			toDo.setType("申请采购");
			toDo.setShenQing(shenQing);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(105);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 105, user1, youJian);
				st.setShenQing(shenQing);
				st.setWuZi(shenQing.getWuZi());
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/cghhPLTY")
	public void cghhPLTY(HttpServletRequest request,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		String hhId[]=request.getParameterValues("hhId");
		String result="{\"result\":\"none\"}";
		if(hhId!=null){
			String root=rootManager.getRoot();
			Email email=emailManager.getEmail();
			for(int i=0;i<hhId.length;i++){
				HuanHuo huanHuo=huanHuoManager.get(hhId[i]);
				ShenQing shenQing=huanHuo.getShenQing();
				shenQing.setHuanJie(huanJieManager.get(203));
				shenQingManager.cover(shenQing);
				ShenQingJL shenQingJL=new ShenQingJL();
				shenQingJL.setShenQing(shenQing);
				shenQingJL.setCaoZuoRQ(new Date());
				shenQingJL.setHuanJie(huanJieManager.get(202));
				shenQingJL.setPiFuLX("通过");
				shenQingJL.setPiFuNR("同意");
				shenQingJL.setUser(user);
				shenQingJLManager.save(shenQingJL);
				huanHuo.setYuanChanPinXLH(shenQing.getWuZi().getChanPinXLH());
				huanHuo.setHuanJie(huanJieManager.get(203));
				huanHuoManager.cover(huanHuo);
				HuanHuoJL huanHuoJL=new HuanHuoJL();
				huanHuoJL.setCaoZuoRQ(new Date());
				huanHuoJL.setHuanHuo(huanHuo);
				huanHuoJL.setHuanJie(huanJieManager.get(202));
				huanHuoJL.setUser(user);
				huanHuoJL.setPiFuLX("采买人同意换货");
				huanHuoJL.setPiFuNR("同意");
				huanHuoJLManager.save(huanHuoJL);
				toDoManager.delete(shenQing, user);
				//低值管理员发放
				for(User user1:userManager.getGZGLY()){
					ToDo toDo=new ToDo();
					toDo.setUser(user1);
					toDo.setHuanJie(huanJieManager.get(203));
					toDo.setCreateDate(new Date());
					toDo.setTask("换货入库");
					toDo.setType("申请采购");
					toDo.setShenQing(shenQing);
					toDoManager.save(toDo);
					//\\发送邮件
					YouJian youJian=youJianManager.get(105);
					if(youJian!=null && youJian.getState()==1){
						SendMailThread st=new SendMailThread(email, root, 105, user1, youJian);
						st.setShenQing(shenQing);
						st.setWuZi(shenQing.getWuZi());
						Thread t=new Thread(st);
						t.start();
					}
				}
			}
		}
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/cghhPL")
	public void cghhPL(HttpServletRequest request,HttpServletResponse response){
		String type[]=request.getParameterValues("hhId");
		String result="{\"result\":\"none\"}";
		if(type!=null)
			result="{\"result\":\"success\"}";
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/cghhBTY")
	public ModelAndView cghhBTY(String id){
		ModelAndView mav=new ModelAndView();
		mav.addObject("hhId", id);
		mav.setViewName("shenqing/cghhBTY");
		return mav;
	}
	
	@RequestMapping(value = "/cghhBTYSave")
	public void cghhBTYSave(String hhId,String boHuiYY,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		HuanHuo huanHuo=huanHuoManager.get(hhId);
		ShenQing shenQing=huanHuo.getShenQing();
		shenQing.setHuanJie(huanJieManager.get(106));
		shenQingManager.cover(shenQing);
		ShenQingJL shenQingJL=new ShenQingJL();
		shenQingJL.setShenQing(shenQing);
		shenQingJL.setCaoZuoRQ(new Date());
		shenQingJL.setHuanJie(huanJieManager.get(202));
		shenQingJL.setPiFuLX("不同意换货");
		shenQingJL.setPiFuNR(boHuiYY);
		shenQingJL.setUser(user);
		shenQingJLManager.save(shenQingJL);
		huanHuo.setHuanJie(huanJieManager.get(203));
		huanHuoManager.cover(huanHuo);
		HuanHuoJL huanHuoJL=new HuanHuoJL();
		huanHuoJL.setCaoZuoRQ(new Date());
		huanHuoJL.setHuanHuo(huanHuo);
		huanHuoJL.setHuanJie(huanJieManager.get(202));
		huanHuoJL.setUser(user);
		huanHuoJL.setPiFuLX("采买人不同意换货");
		huanHuoJL.setPiFuNR(boHuiYY);
		huanHuoJLManager.save(huanHuoJL);
		toDoManager.delete(shenQing, user);
		//低值管理员发放
		String root=rootManager.getRoot();
		Email email=emailManager.getEmail();
		for(User user1:userManager.getGZGLY()){
			ToDo toDo=new ToDo();
			toDo.setUser(user1);
			toDo.setHuanJie(huanJieManager.get(106));
			toDo.setCreateDate(new Date());
			toDo.setTask("物资发放");
			toDo.setType("申请采购");
			toDo.setShenQing(shenQing);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(106);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 106, user1, youJian);
				st.setWuZi(shenQing.getWuZi());
				Thread t=new Thread(st);
				t.start();
			}
		}
		String result="{\"result\":\"success\"}";
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/cghhPLBTY")
	public ModelAndView cghhPLBTY(HttpServletRequest request){
		ModelAndView mav=new ModelAndView();
		String hhId[]=request.getParameterValues("hhId");
		String hhIds="";
		for(int i=0;i<hhId.length;i++){
			hhIds=hhIds+hhId[i]+",";
		}
		if(hhIds.length()>0)
			hhIds=hhIds.substring(0, hhIds.length()-1);
		mav.addObject("hhIds", hhIds);
		mav.setViewName("shenqing/cghhPLBTY");
		return mav;
	}
	
	@RequestMapping(value = "/cghhPLBTYSave")
	public void cghhPLBTYSave(String hhIds,String boHuiYY,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		String root=rootManager.getRoot();
		Email email=emailManager.getEmail();
		String hhId[]=hhIds.split(",");
		for(int i=0;i<hhId.length;i++){
			HuanHuo huanHuo=huanHuoManager.get(hhId[i]);
			ShenQing shenQing=huanHuo.getShenQing();
			shenQing.setHuanJie(huanJieManager.get(106));
			shenQingManager.cover(shenQing);
			ShenQingJL shenQingJL=new ShenQingJL();
			shenQingJL.setShenQing(shenQing);
			shenQingJL.setCaoZuoRQ(new Date());
			shenQingJL.setHuanJie(huanJieManager.get(202));
			shenQingJL.setPiFuLX("不同意换货");
			shenQingJL.setPiFuNR(boHuiYY);
			shenQingJL.setUser(user);
			shenQingJLManager.save(shenQingJL);
			huanHuo.setHuanJie(huanJieManager.get(203));
			huanHuoManager.cover(huanHuo);
			HuanHuoJL huanHuoJL=new HuanHuoJL();
			huanHuoJL.setCaoZuoRQ(new Date());
			huanHuoJL.setHuanHuo(huanHuo);
			huanHuoJL.setHuanJie(huanJieManager.get(202));
			huanHuoJL.setUser(user);
			huanHuoJL.setPiFuLX("采买人不同意换货");
			huanHuoJL.setPiFuNR(boHuiYY);
			huanHuoJLManager.save(huanHuoJL);
			toDoManager.delete(shenQing, user);
			//低值管理员发放
			for(User user1:userManager.getGZGLY()){
				ToDo toDo=new ToDo();
				toDo.setUser(user1);
				toDo.setHuanJie(huanJieManager.get(106));
				toDo.setCreateDate(new Date());
				toDo.setTask("物资发放");
				toDo.setType("申请采购");
				toDo.setShenQing(shenQing);
				toDoManager.save(toDo);
				//\\发送邮件
				YouJian youJian=youJianManager.get(106);
				if(youJian!=null && youJian.getState()==1){
					SendMailThread st=new SendMailThread(email, root, 106, user1, youJian);
					st.setWuZi(shenQing.getWuZi());
					Thread t=new Thread(st);
					t.start();
				}
			}
		}
		String result="{\"result\":\"success\"}";
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/wxhhList")
	public ModelAndView wxhhList(){
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("huanHuoList", huanHuoManager.getAllHuanHuoSQ2CMR(2, user));
		mav.setViewName("weixiu/wxhhListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/weiXiuHuanHuoTY")
	public void weiXiuHuanHuoTY(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		HuanHuo huanHuo=huanHuoManager.get(id);
		WeiXiu weiXiu=huanHuo.getWeiXiu();
		weiXiu.setHuanJie(huanJieManager.get(204));
		weiXiuManager.cover(weiXiu);
		WeiXiuJL weiXiuJL=new WeiXiuJL();
		weiXiuJL.setCaoZuoRQ(new Date());
		weiXiuJL.setHuanJie(huanJieManager.get(202));
		weiXiuJL.setPiFuLX("采买人同意换货");
		weiXiuJL.setPiFuNR("同意");
		weiXiuJL.setUser(user);
		weiXiuJLManager.save(weiXiuJL);
		huanHuo.setYuanChanPinXLH(weiXiu.getWuZi().getChanPinXLH());		
		huanHuo.setHuanJie(huanJieManager.get(204));
		huanHuoManager.cover(huanHuo);
		HuanHuoJL huanHuoJL=new HuanHuoJL();
		huanHuoJL.setCaoZuoRQ(new Date());
		huanHuoJL.setHuanHuo(huanHuo);
		huanHuoJL.setHuanJie(huanJieManager.get(202));
		huanHuoJL.setUser(user);
		huanHuoJL.setPiFuLX("采买人同意换货");
		huanHuoJL.setPiFuNR("同意");
		huanHuoJLManager.save(huanHuoJL);
		toDoManager.deleteList(weiXiu);
		//\\发送邮件
		String root=rootManager.getRoot();
		Email email=emailManager.getEmail();
		for(User user1:userManager.getGZGLY()){
			YouJian youJian=youJianManager.get(203);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 203, user1, youJian);
				st.setWuZi(huanHuo.getWuZi());
				st.setHuanHuo(huanHuo);
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/wxhhPLTY")
	public void wxhhPLTY(HttpServletRequest request,HttpServletResponse response){
		String result="{\"result\":\"none\"}";
		String wxhhId[]=request.getParameterValues("wxhhId");
		if(wxhhId!=null){
			Subject currentUser = SecurityUtils.getSubject();
			Object obj=currentUser.getPrincipals();
			String name=obj.toString();
			User user=userManager.findByUsername(name);
			String root=rootManager.getRoot();
			Email email=emailManager.getEmail();
			for(int i=0;i<wxhhId.length;i++){
				HuanHuo huanHuo=huanHuoManager.get(wxhhId[i]);
				WeiXiu weiXiu=huanHuo.getWeiXiu();
				weiXiu.setHuanJie(huanJieManager.get(204));
				weiXiuManager.cover(weiXiu);
				WeiXiuJL weiXiuJL=new WeiXiuJL();
				weiXiuJL.setCaoZuoRQ(new Date());
				weiXiuJL.setHuanJie(huanJieManager.get(202));
				weiXiuJL.setPiFuLX("采买人同意换货");
				weiXiuJL.setPiFuNR("同意");
				weiXiuJL.setUser(user);
				weiXiuJLManager.save(weiXiuJL);
				huanHuo.setYuanChanPinXLH(weiXiu.getWuZi().getChanPinXLH());		
				huanHuo.setHuanJie(huanJieManager.get(204));
				huanHuoManager.cover(huanHuo);
				HuanHuoJL huanHuoJL=new HuanHuoJL();
				huanHuoJL.setCaoZuoRQ(new Date());
				huanHuoJL.setHuanHuo(huanHuo);
				huanHuoJL.setHuanJie(huanJieManager.get(202));
				huanHuoJL.setUser(user);
				huanHuoJL.setPiFuLX("采买人同意换货");
				huanHuoJL.setPiFuNR("同意");
				huanHuoJLManager.save(huanHuoJL);
				toDoManager.deleteList(weiXiu);
				for(User user1:userManager.getGZGLY()){
					YouJian youJian=youJianManager.get(203);
					if(youJian!=null && youJian.getState()==1){
						SendMailThread st=new SendMailThread(email, root, 203, user1, youJian);
						st.setWuZi(huanHuo.getWuZi());
						st.setHuanHuo(huanHuo);
						Thread t=new Thread(st);
						t.start();
					}
				}
			}
			result="{\"result\":\"success\"}";
		}
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value="/wxhhBTY")
	public ModelAndView wxhhBTY(String id){
		ModelAndView mav=new ModelAndView();
		mav.addObject("wxhhId", id);
		mav.setViewName("weixiu/wxhhBTY");
		return mav;
	}
	
	@RequestMapping(value="/wxhhBTYSave")
	public void wxhhBTYSave(String wxhhId,String boHuiYY,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		HuanHuo huanHuo=huanHuoManager.get(wxhhId);
		WeiXiu weiXiu=huanHuo.getWeiXiu();
		weiXiu.setHuanJie(huanJieManager.get(402));
		weiXiuManager.cover(weiXiu);
		WeiXiuJL weiXiuJL=new WeiXiuJL();
		weiXiuJL.setCaoZuoRQ(new Date());
		weiXiuJL.setHuanJie(huanJieManager.get(202));
		weiXiuJL.setPiFuLX("采买人同意换货");
		weiXiuJL.setPiFuNR("同意");
		weiXiuJL.setUser(user);
		weiXiuJLManager.save(weiXiuJL);
		huanHuo.setHuanJie(huanJieManager.get(204));
		huanHuoManager.cover(huanHuo);
		HuanHuoJL huanHuoJL=new HuanHuoJL();
		huanHuoJL.setCaoZuoRQ(new Date());
		huanHuoJL.setHuanHuo(huanHuo);
		huanHuoJL.setHuanJie(huanJieManager.get(202));
		huanHuoJL.setUser(user);
		huanHuoJL.setPiFuLX("采买人不同意换货");
		huanHuoJL.setPiFuNR(boHuiYY);
		huanHuoJLManager.save(huanHuoJL);
		String root=rootManager.getRoot();
		Email email=emailManager.getEmail();
		for(User user1:userManager.getGZGLY()){
			ToDo toDo=new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(402));
			toDo.setTask("维修审批");
			toDo.setType("维修");
			toDo.setUser(user1);
			toDo.setWeiXiu(weiXiu);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(402);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 402, user1, youJian);
				st.setWeiXiu(weiXiu);
				st.setWuZi(weiXiu.getWuZi());
				Thread t=new Thread(st);
				t.start();
			}
		}
		String result="{\"result\":\"success\"}";
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/wxhhPL")
	public void wxhhPL(HttpServletRequest request,HttpServletResponse response){
		String wxhhId[]=request.getParameterValues("wxhhId");
		String result="{\"result\":\"none\"}";
		if(wxhhId!=null){
			result="{\"result\":\"success\"}";
		}
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/wxhhPLBTY")
	public ModelAndView wxhhPLBTY(HttpServletRequest request){
		ModelAndView mav=new ModelAndView();
		String wxhhId[]=request.getParameterValues("wxhhId");
		String wxhhIds="";
		if(wxhhId!=null){
			for(int i=0;i<wxhhId.length;i++){
				wxhhIds=wxhhIds+wxhhId[i]+",";
			}
			if(wxhhIds.length()>0)
				wxhhIds=wxhhIds.substring(0, wxhhIds.length());
		}
		mav.addObject("wxhhIds", wxhhIds);
		mav.setViewName("weixiu/wxhhPLBTY");
		return mav;
	}
	
	@RequestMapping(value="/wxhhPLBTYSave")
	public void wxhhPLBTY(String wxhhIds,String boHuiYY,HttpServletResponse response){
		String wxhhId[]=wxhhIds.split(",");
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		String root=rootManager.getRoot();
		Email email=emailManager.getEmail();
		for(int i=0;i<wxhhId.length;i++){
			HuanHuo huanHuo=huanHuoManager.get(wxhhId[i]);
			WeiXiu weiXiu=huanHuo.getWeiXiu();
			weiXiu.setHuanJie(huanJieManager.get(402));
			weiXiuManager.cover(weiXiu);
			WeiXiuJL weiXiuJL=new WeiXiuJL();
			weiXiuJL.setCaoZuoRQ(new Date());
			weiXiuJL.setHuanJie(huanJieManager.get(202));
			weiXiuJL.setPiFuLX("采买人同意换货");
			weiXiuJL.setPiFuNR("同意");
			weiXiuJL.setUser(user);
			weiXiuJLManager.save(weiXiuJL);
			huanHuo.setHuanJie(huanJieManager.get(204));
			huanHuoManager.cover(huanHuo);
			HuanHuoJL huanHuoJL=new HuanHuoJL();
			huanHuoJL.setCaoZuoRQ(new Date());
			huanHuoJL.setHuanHuo(huanHuo);
			huanHuoJL.setHuanJie(huanJieManager.get(202));
			huanHuoJL.setUser(user);
			huanHuoJL.setPiFuLX("采买人不同意换货");
			huanHuoJL.setPiFuNR(boHuiYY);
			huanHuoJLManager.save(huanHuoJL);
			for(User user1:userManager.getGZGLY()){
				ToDo toDo=new ToDo();
				toDo.setCreateDate(new Date());
				toDo.setHuanJie(huanJieManager.get(402));
				toDo.setTask("维修审批");
				toDo.setType("维修");
				toDo.setUser(user1);
				toDo.setWeiXiu(weiXiu);
				toDoManager.save(toDo);
				//\\发送邮件
				YouJian youJian=youJianManager.get(402);
				if(youJian!=null && youJian.getState()==1){
					SendMailThread st=new SendMailThread(email, root, 402, user1, youJian);
					st.setWeiXiu(weiXiu);
					st.setWuZi(weiXiu.getWuZi());
					Thread t=new Thread(st);
					t.start();
				}
			}
		}
		String result="{\"result\":\"success\"}";
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/hhrkList")
	public ModelAndView hhrkList(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("huanHuoRK", weiXiuManager.getAllWeiXiuByZT(204));
		mav.setViewName("weixiu/hhrkListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/huanHuoRK")
	public ModelAndView huanHuoRK(String id){
		ModelAndView mav=new ModelAndView();
		mav.addObject("weiXiu", weiXiuManager.get(id));
		mav.setViewName("weixiu/huanHuoRK");
		return mav;
	}
	
	@RequestMapping(value = "/huanHuoRKSave")
	public void huanHuoRKSave(String id,String chanPinXLH,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		WeiXiu weiXiu=weiXiuManager.get(id);		
		HuanHuo huanHuo=huanHuoManager.getByWeiXiu(weiXiu);
		huanHuo.setXinChanPinXLH(chanPinXLH);
		huanHuo.setHuanJie(huanJieManager.get(404));
		huanHuoManager.cover(huanHuo);
		HuanHuoJL huanHuoJL=new HuanHuoJL();
		huanHuoJL.setCaoZuoRQ(new Date());
		huanHuoJL.setHuanHuo(huanHuo);
		huanHuoJL.setHuanJie(huanJieManager.get(204));
		huanHuoJL.setPiFuLX("维修换货入库");
		huanHuoJL.setPiFuNR("同意");
		huanHuoJL.setUser(user);
		huanHuoJLManager.save(huanHuoJL);
		weiXiu.setHuanJie(huanJieManager.get(404));
		weiXiuManager.cover(weiXiu);
		WeiXiuJL weiXiuJL=new WeiXiuJL();
		weiXiuJL.setCaoZuoRQ(new Date());
		weiXiuJL.setHuanJie(huanJieManager.get(204));
		weiXiuJL.setPiFuLX("维修换货入库");
		weiXiuJL.setPiFuNR("同意");
		weiXiuJL.setUser(user);
		weiXiuJL.setWeiXiu(weiXiu);
		weiXiuJLManager.save(weiXiuJL);
		WuZi wuZi=weiXiu.getWuZi();
		wuZi.setChanPinXLH(chanPinXLH);
		wuZi.setShiYongZT(shiYongZTManager.get(114));
		wuZiManager.cover(wuZi);
		
		//待使用人确认
		toDoManager.deleteList(weiXiu);
		ToDo toDo=new ToDo();
		toDo.setCreateDate(new Date());
		toDo.setHuanJie(huanJieManager.get(404));
		toDo.setTask("维修确认");
		toDo.setType("维修");
		toDo.setUser(wuZi.getUser());
		toDo.setWeiXiu(weiXiu);
		toDoManager.save(toDo);
		//\\发送邮件
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		YouJian youJian=youJianManager.get(404);
		if(youJian!=null && youJian.getState()==1){
			SendMailThread st=new SendMailThread(email, root, 404, wuZi.getUser(), youJian);
			st.setWeiXiu(weiXiu);
			st.setWuZi(weiXiu.getWuZi());
			Thread t=new Thread(st);
			t.start();
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
